package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {



    /**
     * 容器中的组件 Binding、Queue、Exchange都会自动创建（RabbitMQ没有的情况）
     * RabbitMQ 只要有。属性发生变化也不会覆盖
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String,Object> arguments=new HashMap<>();

        //死信路由
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        //死信路由件
        arguments.put("x-dead-letter-routing-key","order.release.order");
        //消息过期时间
        arguments.put("x-message-ttl",60000);
        /**
         * 参数1：队列名字
         * 参数2：是否持久化
         * 参数3：是否排他
         * 参数4：是否自动删除
         * 参数5：其他一些参数
         */
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);
        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueu() {
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    @Bean
    public Exchange orderEventExchange() {
        /**
         * String name, 交换机名称
         * boolean durable, 是否持久化
         * boolean autoDelete, 是否自动删除
         * Map<String, Object> arguments 其他一些参数
         */
        return new TopicExchange("order-event-exchange",true,false);
    }

    @Bean
    public Binding orderCreateOrder() {
        /**
         * String destination, 目的地
         * DestinationType destinationType, 目的地类型
         * String exchange, 绑定的交换机
         * String routingKey, 路由键
         * @Nullable Map<String, Object> arguments 其他参数
         */
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseOrderBingding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue(){
        return new Queue("order.seckill.order.queue",true,false,false);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(){
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }

}
